package com.topvision.camerasdk.album;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;

import com.topvision.camerasdk.R;
import com.topvision.camerasdk.camera.CameraConstants;
import com.topvision.camerasdk.video.PlayVideoActivity;
import com.topvision.commomsdk.TouchInterface;
import com.topvision.commomsdk.commom.Album;
import com.topvision.commomsdk.commom.PhotoMenu;
import com.topvision.commomsdk.commom.PhotoMenuFragment;
import com.topvision.commomsdk.commom.PhotoMenuFragmentAdapter;
import com.topvision.commomsdk.uitl.AndroidUtils;
import com.topvision.commomsdk.uitl.FileUtil;
import com.topvision.commomsdk.view.TvViewPager;
import com.topvision.voicesdk.BaseVoiceFragmentActivity;
import com.topvision.voicesdk.CommonVoice;
import com.topvision.voicesdk.VoiceInterface;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class PhotoMenuActivity extends BaseVoiceFragmentActivity implements
        TouchInterface, VoiceInterface {
    //删除照片
    private final int DELETE_PHOTO = 0;
    //操作间隔时长,显示菜单的时间 和删除菜单时间必须要大于1秒才允许进行操作，避免一些误操作
    private final int INTERVAL_TIME = 300;

    private TvViewPager mViewPager;
    private PhotoMenuFragmentAdapter photoMenuAdapter;
    private List<Fragment> list;
    //一些菜单信息
    List<PhotoMenu> photoMenus;
    //当前菜单索引
    private int currentItem = 0;
    //当前图片对象
    Album album;
    Context context;
    //照片菜单显示的时长
    public long showMenuTime = 0;

    private String filePath;
    private boolean deleteIsSuccess = false;
    private String deleteFileType;
    private View mCancelView;
    private View mDeleteView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        overridePendingTransition(R.anim.slide_up_in, R.anim.slide_down_out);
        setContentView(R.layout.activity_camera_photo_menu);
        context = this;
        initView();
        initData();
    }

    private void initData() {
        list = new ArrayList<Fragment>();
        showMenuTime = getIntent().getLongExtra(ShowPicActivity.SHOW_MENU_TIME, System.currentTimeMillis());
        album = getIntent().getParcelableExtra(ShowPicActivity.ALBUM_KEY);
        filePath = album.getPath();/***   12.13 添加*/
        photoMenus = new ArrayList<>();
        PhotoMenu photoMenu1 = new PhotoMenu(getString(R.string.delete_photo), R.mipmap.photo_menu_delete);
        photoMenus.add(photoMenu1);

//        PhotoMenu photoMenu12 = new PhotoMenu("分享", R.mipmap.ic_launcher);
//        photoMenus.add(photoMenu12);
//
//        PhotoMenu photoMenu13 = new PhotoMenu("蓝牙发送", R.mipmap.ic_launcher);
//        photoMenus.add(photoMenu13);

        //构建Fragment
        for (PhotoMenu photoMenu : photoMenus) {
            list.add(PhotoMenuFragment.newInstance(photoMenu.getMenuName(), photoMenu.getResourceId()));
        }

//        mViewPager.setOnPageChangeListener(this);
        mViewPager = (TvViewPager) AndroidUtils.setAnimation(mViewPager);
        photoMenuAdapter = new PhotoMenuFragmentAdapter(
                getSupportFragmentManager(), list);
        mViewPager.setAdapter(photoMenuAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        setTouchInterfaceListener(this);
        setVoiceInterfaceListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        setTouchInterfaceListener(null);
        setVoiceInterfaceListener(null);
    }


    private void initView() {
        mViewPager = (TvViewPager) findViewById(R.id.photo_menu_viewpager);
        mCancelView = findViewById(R.id.fl_cancel);
        mDeleteView = findViewById(R.id.fl_delete);

        if (AndroidUtils.isBtDevice()) {
            mViewPager.setNoScroll(false);
            mCancelView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
            mDeleteView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    deletePhoto();
                }
            });
        } else {
            mViewPager.setNoScroll(true);
            isDeleted(false);
        }

    }


    @Override
    public void dealWithVoice(String data) {
        if (CommonVoice.VOICE_ZH_DELETE.equals(data) || CommonVoice.VOICE_US_DELETE.equalsIgnoreCase(data)) {
            deletePhoto();
        } else if (CommonVoice.VOICE_ZH_BACK.equals(data) || CommonVoice.VOICE_US_BACK.equalsIgnoreCase(data)) {
            finish();
        }
    }


    @Override
    public void doForwardTouch() {
        if (!AndroidUtils.isBtDevice()) {
            isDeleted(true);
            currentItem = mViewPager.getCurrentItem();
            if (currentItem > list.size() - 1) {
                return;
            }
            currentItem++;
            mViewPager.setCurrentItem(currentItem);
        }
    }

    private void isDeleted(boolean flag) {
        mCancelView.setSelected(!flag);
        mDeleteView.setSelected(flag);
    }

    @Override
    public void doBackwardTouch() {
        if (!AndroidUtils.isBtDevice()) {
            isDeleted(false);
            currentItem = mViewPager.getCurrentItem();
            if (currentItem <= 0) {
                return;
            }
            currentItem--;
            mViewPager.setCurrentItem(currentItem);
        }

    }

    @Override
    public void doClickTouch() {
        if (!AndroidUtils.isBtDevice()) {
            chooseMenu();
        }
    }

    @Override
    public void doDoubleTouch() {

    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.slide_up_in, R.anim.slide_down_out);
    }

    /**
     * 选择某个菜单功能
     *
     * @author jack
     * create at 2016/11/23 1744
     */
    private void chooseMenu() {
        if (mDeleteView.isSelected()) {
            deletePhoto();
        } else {
            finish();
        }
        switch (currentItem) {
            case DELETE_PHOTO:
//                deletePhoto();
                break;
            default:
                break;
        }
    }


    private void deletePhoto() {
        if (album == null) {
            return;
        }

       /* if(System.currentTimeMillis() - showMenuTime > INTERVAL_TIME ){
            //刪除原始图片和缩略图
            boolean albumFlag = FileUtil.removeFile(album.getPath());
            boolean thumbnailFlag = FileUtil.removeFile(album.getThumbnailPath());
            boolean isSuccess = false;
            if (!albumFlag && !thumbnailFlag) {
                Toast.makeText(context, "删除失败", Toast.LENGTH_LONG).show();
            } else {
                isSuccess = true;
            }
            sendUpdatePhotoBroadcast(isSuccess);
            //结束当前菜单视频
            finish();
            //关闭照片预览页面
            if (ShowPicActivity.showPicActivity == null) {
                ShowPicActivity.showPicActivity = new ShowPicActivity();
            }
            ShowPicActivity.showPicActivity.finish();
        }


    }

    private void sendUpdatePhotoBroadcast(boolean isSuccess) {
        Intent intent = new Intent(MyAblumActivity.ACTION_UPDATE_ALBUM);
        sendBroadcast(intent);
    }
*/
        //为的是过快点击勿删除操作
        if (System.currentTimeMillis() - showMenuTime > INTERVAL_TIME) {
            if (!TextUtils.isEmpty(filePath)) {
                if (filePath.contains(".jpg")) {
                    //刪除原始图片和缩略图
                    boolean albumFlag = FileUtil.removeFile(context, album.getPath());
                    boolean thumbnailFlag = FileUtil.removeFile(context, album.getThumbnailPath());
                    if (!albumFlag && !thumbnailFlag) {
                        AndroidUtils.showTips(this, getString(R.string.delete_failed));
                    } else {
                        deleteIsSuccess = true;
                        deleteFileType = "photo";
                    }
                }//删除视频
                else if (filePath.contains(".mp4")) {
                    String path = album.getPath();
                    String[] split = path.split("\\/");
                    String s = split[split.length - 1];
                    String videoThumbnailPath = s.replace(".mp4", "-video.jpg");
                    String thumbnailPath = CameraConstants.THUMBNAIL_FOLDER + File.separator + videoThumbnailPath;
                    boolean videoFlag = FileUtil.removeFile(context, album.getPath());
                    boolean videoThumbnailFlag = FileUtil.removeFile(context, thumbnailPath);
                    if (!videoFlag && !videoThumbnailFlag) {
                        AndroidUtils.showTips(this, getString(R.string.delete_failed));
                    } else {
                        deleteIsSuccess = true;
                        deleteFileType = "video";
                    }
                }
                sendUpdateFileBroadcast(deleteIsSuccess, deleteFileType);
            }

            //结束当前菜单
            this.finish();
        }

    }

    private void sendUpdateFileBroadcast(boolean deleteFileTag, String deleteFileType) {
        if (deleteFileTag && !deleteFileType.equals("")) {

            if (deleteFileType.equals("photo")) {
                Intent intent = new Intent(MyAblumActivity.ACTION_UPDATE_ALBUM);
                //向照片列表发送广播
                //关闭照片预览页面
                if (ShowPicActivity.showPicActivity == null) {
                    ShowPicActivity.showPicActivity = new ShowPicActivity();
                }
                ShowPicActivity.showPicActivity.finish();
                sendBroadcast(intent);
            } else if (deleteFileType.equals("video")) {
                Intent intent = new Intent(MyAblumActivity.ACTION_UPDATE_ALBUM);
                //向视频列表发送广播
                //关闭预览页面
                if (PlayVideoActivity.playVideoActivity == null) {
                    PlayVideoActivity.playVideoActivity = new PlayVideoActivity();
                }
                PlayVideoActivity.playVideoActivity.finish();
                sendBroadcast(intent);
            }

        }
    }

}
